<template>
    <view class="content">
        <view class="item"><input type="text" placeholder="用户名" v-model="phone"></view>
        <view class="item"><input type="text" placeholder="密码" v-model="password"></view>
        <view class="loginclick" @click="login"><button type="primary">登录</button></view>
        <view class="zhuclick"  @click="zhu"><button type="">注册</button></view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            phone: '18242000511',
            password: '000511',
            users: []
        }
    },
    onShow() {

    },
    methods: {
        // 登录功能
        login() {
            if (!this.phone || !this.password) {
                uni.showToast({
                    title: '用户名和密码不能为空',
                    icon: 'none'
                });
                return;
            }

            uni.showLoading({
                title: '登录中...',
                mask: true
            });

            uni.request({
                url: `http://localhost:3000/users?phone=${this.phone}`,
                success: (res) => {
                    uni.hideLoading();
                    this.users = res.data;
                    if (this.users.length > 0) {
                        if (this.password === this.users[0].password) {
                            // 存储登录用户信息
                            uni.setStorageSync('currentUser', this.users[0]);
                            // 存储用户ID，用于后续数据区分
                            uni.setStorageSync('userId', this.users[0].id);

                            // 显示登录成功提示
                            uni.showToast({
                                title: '登录成功',
                                icon: 'success',
                                duration: 1500,
                                success: () => {
                                    // 提示显示完成后跳转
                                    setTimeout(() => {
                                        uni.switchTab({
                                            url: '/pages/own/index'
                                        });
                                    }, 300); // 稍微延迟300ms确保动画完成
                                }
                            });
                        } else {
                            uni.showToast({
                                title: '密码错误',
                                icon: 'none'
                            });
                        }
                    } else {
                        uni.showToast({
                            title: '用户不存在',
                            icon: 'none'
                        });
                    }
                }
            });
        },
        zhu() {
            uni.navigateTo({
                url: `/pages/login/zhu`
            })
        }
    }
}
</script>

<style scoped>
.content {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.item {
    width: 50%;
    height: 40px;
    border: 1px solid rgb(245, 232, 232);
    margin-top: 20px;
    background-color: aliceblue;
    padding-top: 10px;
}

.item input {

}

.loginclick {
    width: 50%;
    height: 40px;
    margin-top: 20px;
}

.zhuclick {
    margin-top: 20px;
    width: 50%;
    height: 40px;
    background-color: aliceblue;
}
</style>    